package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.context.BaseContext;
import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.entity.Dish;
import com.sky.exception.BusinessException;
import com.sky.mapper.CategoryMapper;
import com.sky.mapper.DishMapper;
import com.sky.result.PageResult;
import com.sky.service.CategoryService;
import com.sky.utils.BeanHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.beans.beancontext.BeanContext;
import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@Service
public class CategoryServiceImpl implements CategoryService {

    @Resource
    private CategoryMapper categoryMapper;

    @Resource
    private DishMapper dishMapper;

    // 新增分类
    @Override
    public void save(CategoryDTO categoryDTO) {
        //1. 补全实体属性
        Category category = BeanHelper.copyProperties(categoryDTO, Category.class);
        if (category.getSort() != null){
            if (!(category.getSort() >= 0 && category.getSort() <= 99)){
                throw new BusinessException(MessageConstant.CATEGORY_BY_SORT_RANGE_INTEGER);
            }
        } else {
            throw new BusinessException(MessageConstant.CATEGORY_BY_SORT);
        }

        //1.1 设置分类状态 默认: 启用
        category.setStatus(StatusConstant.ENABLE);

//        //1.2 设置创建时间 设置成了公共属性
//        category.setCreateTime(LocalDateTime.now());
//
//        //1.3 设置更新时间
//        category.setUpdateTime(LocalDateTime.now());
//
//        //1.4 设置创建人
//        category.setCreateUser(BaseContext.getCurrentId());
//
//        //1.5 设置修改人
//        category.setUpdateUser(BaseContext.getCurrentId());

        //2. 调用mapper，保存数据
        categoryMapper.insert(category);
    }

    // 分类分页查询
    @Override
    public PageResult page(CategoryPageQueryDTO pageQueryDTO) {
        //1.设置分页参数
        PageHelper.startPage(pageQueryDTO.getPage(), pageQueryDTO.getPageSize());

        //2.执行查询
        List<Category> categoryList = categoryMapper.list(pageQueryDTO.getName(), pageQueryDTO.getType());
//        List<Category> categoryList = categoryMapper.list();
        //判断输入的名称是否存在
        if (categoryList.isEmpty()){
            throw new BusinessException(MessageConstant.CATEGORY_BY_NAME);
        }

        //3.解析并封装结果
        // 将分页查询的结果，封装到 PageInfo
        // PageInfo 对象包含了分页的各个信息，比如当前页面 pageQueryDTO.getPage(), 共有多少条记录
        PageInfo pageInfo = new PageInfo(categoryList, pageQueryDTO.getPageSize());
//        Page<Category> categoryPage = (Page<Category>) categoryList;

        //4.返回结果
//        return new PageResult(categoryPage.getTotal(),categoryPage.getResult());
        return new PageResult(pageInfo.getTotal(), pageInfo.getList());
    }

    /**
     * @description: 根据id删除分类
     * @author: tanbobin
     * @date: 2024/5/31 17:36
     * @param:
     * @param id
     * @return:
     **/
    @Transactional
    @Override
    public void deleteById(Long id) {
        // 1.  判断分类下是否有产品
//        Long count = dishMapper.queryDishById(id);
        List<String> nameList = dishMapper.queryById(id);
        if (!ObjectUtils.isEmpty(nameList)){ // 该分类有菜品
            throw new BusinessException(MessageConstant.CATEGORY_BE_RELATED_BY_DISH);
        }

        categoryMapper.deleteById(id);

        // 2. 判断分类的状态
//        Long countStatus = categoryMapper.queryById(id);
//        if (countStatus > 0){ // 该分类不是禁用状态 不能删除
//            throw new BusinessException(MessageConstant.)
//        }


    }

    /**
     * @description: 根据分类id查询数据
     * @author: tanbobin
     * @date: 2024/6/1 15:45
     * @param:
     * @param id
     * @return:
     * @return com.sky.entity.Category
     **/
    @Override
    public Category queryById(Long id) {
        Category category = categoryMapper.queryCategoryById(id);
        return category;
    }

    /**
     * @description: 修改分类数据
     * @author: tanbobin
     * @date: 2024/6/1 16:19
     * @param:
     * @param categoryDTO
     * @return:
     **/
    @Override
    public void update(CategoryDTO categoryDTO) {
        // 补全实体属性
        Category category = BeanHelper.copyProperties(categoryDTO, Category.class);
        categoryMapper.update(category);
    }

    /**
     * @description: 禁用/启用分类
     * @author: tanbobin
     * @date: 2024/6/3 10:23
     * @param:
     * @param status
     * @param id
     * @return:
     **/
    @Override
    public void disableOrEnable(Integer status, Long id) {
        Category category = new Category();
        category.setStatus(status);
        category.setId(id);
        categoryMapper.update(category);
    }

    /**
     * @description: C端-查询分类
     * @author: tanbobin
     * @date: 2024/6/5 9:49
     * @param:
     * @param type
     * @return:
     * @return java.util.List<com.sky.entity.Category>
     **/
    @Override
    public List<Category> list(Integer type) {
        return categoryMapper.queryListType(type);
    }
}
